package cn.stylefeng.guns.modular.weixin.config;

import org.springframework.boot.context.properties.*;
import cn.stylefeng.guns.modular.weixin.handler.*;
import org.springframework.beans.factory.annotation.*;
import me.chanjar.weixin.mp.api.impl.*;
import me.chanjar.weixin.mp.config.impl.*;
import org.apache.commons.lang3.*;
import cn.stylefeng.guns.modular.weixin.utils.*;
import java.util.stream.*;
import java.util.function.*;
import java.util.*;
import org.springframework.context.annotation.*;
import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.config.*;
import com.google.common.collect.*;

@Configuration
@EnableConfigurationProperties({ WxMpProperties.class })
public class WxMpConfiguration
{
    private LogHandler logHandler;
    private NullHandler nullHandler;
    private KfSessionHandler kfSessionHandler;
    private StoreCheckNotifyHandler storeCheckNotifyHandler;
    private LocationHandler locationHandler;
    private MenuHandler menuHandler;
    private MsgHandler msgHandler;
    private UnsubscribeHandler unsubscribeHandler;
    private SubscribeHandler subscribeHandler;
    private ScanHandler scanHandler;
    private WxMpProperties properties;
    private static Map<String, WxMpMessageRouter> routers;
    private static Map<String, WxMpService> mpServices;
    
    @Autowired
    public WxMpConfiguration(final LogHandler logHandler, final NullHandler nullHandler, final KfSessionHandler kfSessionHandler, final StoreCheckNotifyHandler storeCheckNotifyHandler, final LocationHandler locationHandler, final MenuHandler menuHandler, final MsgHandler msgHandler, final UnsubscribeHandler unsubscribeHandler, final SubscribeHandler subscribeHandler, final ScanHandler scanHandler, final WxMpProperties properties) {
        this.logHandler = logHandler;
        this.nullHandler = nullHandler;
        this.kfSessionHandler = kfSessionHandler;
        this.storeCheckNotifyHandler = storeCheckNotifyHandler;
        this.locationHandler = locationHandler;
        this.menuHandler = menuHandler;
        this.msgHandler = msgHandler;
        this.unsubscribeHandler = unsubscribeHandler;
        this.subscribeHandler = subscribeHandler;
        this.scanHandler = scanHandler;
        this.properties = properties;
    }
    
    @Bean
    public WxMpService wxMpService() {
        final List<WxMpProperties.MpConfig> configs = (List<WxMpProperties.MpConfig>)this.properties.getConfigs();
        if (configs == null) {
            throw new RuntimeException("\u5927\u54e5\uff0c\u62dc\u6258\u5148\u770b\u4e0b\u9879\u76ee\u9996\u9875\u7684\u8bf4\u660e\uff08readme\u6587\u4ef6\uff09\uff0c\u6dfb\u52a0\u4e0b\u76f8\u5173\u914d\u7f6e\uff0c\u6ce8\u610f\u522b\u914d\u9519\u4e86\uff01");
        }
        final WxMpService service = (WxMpService)new WxMpServiceImpl();


        service.setMultiConfigStorages((Map)configs.stream().map(a -> {
            WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
            configStorage.setAppId(a.getAppId());
            configStorage.setSecret(a.getSecret());
            configStorage.setToken(a.getToken());
            configStorage.setAesKey(a.getAesKey());
            String appName = a.getAppName();
            String weixinNo = a.getWeixinNo();
            Boolean defaultApp = a.getDefaultApp();
            if (!ObjectUtils.allNotNull(new Object[] { appName, weixinNo, defaultApp })) {
                throw new RuntimeException("\u8bf7\u914d\u7f6e\u516c\u4f17\u53f7appName,weixinNo,defaultApp");
            }
            else {
                WxUtils.wechatAppConfig.put(appName, a.getAppId());
                WxUtils.wechatAppConfig.put(a.getAppId(), appName);
                WxUtils.wechatAppConfig.put(weixinNo, a.getAppId());
                WxUtils.wechatAppConfig.put(appName + "," + a.getAppId(), weixinNo);
                if (defaultApp) {
                    WxUtils.wechatAppConfig.put("default", a.getAppId());
                }
                return configStorage;
            }
        }).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o)));
        return service;
    }
    
    @Bean
    public WxMpMessageRouter messageRouter(final WxMpService wxMpService) {
        final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);
        newRouter.rule().handler((WxMpMessageHandler)this.logHandler).next();
        newRouter.rule().async(false).msgType("event").event("kf_create_session").handler((WxMpMessageHandler)this.kfSessionHandler).end();
        newRouter.rule().async(false).msgType("event").event("kf_close_session").handler((WxMpMessageHandler)this.kfSessionHandler).end();
        newRouter.rule().async(false).msgType("event").event("kf_switch_session").handler((WxMpMessageHandler)this.kfSessionHandler).end();
        newRouter.rule().async(false).msgType("event").event("poi_check_notify").handler((WxMpMessageHandler)this.storeCheckNotifyHandler).end();
        newRouter.rule().async(false).msgType("event").event("click").handler((WxMpMessageHandler)this.menuHandler).end();
        newRouter.rule().async(false).msgType("event").event("view").handler((WxMpMessageHandler)this.nullHandler).end();
        newRouter.rule().async(false).msgType("event").event("subscribe").handler((WxMpMessageHandler)this.subscribeHandler).end();
        newRouter.rule().async(false).msgType("event").event("unsubscribe").handler((WxMpMessageHandler)this.unsubscribeHandler).end();
        newRouter.rule().async(false).msgType("event").event("LOCATION").handler((WxMpMessageHandler)this.locationHandler).end();
        newRouter.rule().async(false).msgType("location").handler((WxMpMessageHandler)this.locationHandler).end();
        newRouter.rule().async(false).msgType("event").event("SCAN").handler((WxMpMessageHandler)this.scanHandler).end();
        newRouter.rule().async(false).handler((WxMpMessageHandler)this.msgHandler).end();
        return newRouter;
    }
    
    static {
        WxMpConfiguration.routers = Maps.newHashMap();
        WxMpConfiguration.mpServices = Maps.newHashMap();
    }
}
